﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
using System.IO;
using PaperSystem.Models;
using Microsoft.Office.Interop.Word;//引入word类库

namespace PaperSystem.Models
{
    public class PaperRepository
    {
        PaperDatabaseEntities db;
        public PaperRepository()
        {
            db = new PaperDatabaseEntities();
        }

        //根据信息获取试卷显示列表
        public List<PaperList> GetPaperList(PaperQuery info)
        {
            var list = (from a in db.Paper
                        select new PaperList
                        {
                            course = a.Course.Cname,
                            createTime=a.Create_time,
                            creator=a.Teacher.Tname,
                            paperTitle=a.Title,
                            pid=a.Pid,
                            TorE=a.TorE,
                            usedMinute=a.Minute
                        });
            if (info != null)
            {
                if (info.course != null)
                {
                    list = list.Where(a => a.course == info.course);
                }
                if (info.creator != null)
                {
                    list = list.Where(a => a.creator.Contains(info.creator));
                }
                if (info.paperTitle != null)
                {
                    list = list.Where(a => a.paperTitle.Contains(info.paperTitle));
                }
                if (info.TorE != null)
                {
                    list = list.Where(a => a.TorE == info.TorE);
                }
            }
            return list.ToList();
        }

        //获取准备添加到试卷题目的信息
        public UpdateQuestion GetUpdateQuestion(int eid)
        {
            try
            {
                UpdateQuestion question = new UpdateQuestion();
                //var exq = db.Ex_question.Where(a => a.Eid == eid).FirstOrDefault();//这样得来的exq关联信息会空
                //var inq = db.In_question.Where(a => a.Ex_question.Eid == eid).ToList();//这样得来的inq关联信息会空
                var exq = (from a in db.Ex_question
                           where a.Eid == eid
                           select new
                           {
                               a.Chapter,
                               a.Course,
                               a.Econtent,
                               a.Eid,
                               a.Enum,
                               a.Etime,
                               a.Etype,
                               a.Knowledge,
                               a.Teacher,
                               a.TorE
                           }).FirstOrDefault();
                var inq = (from a in db.In_question
                           where a.Ex_question.Eid == eid
                           select new
                           {
                               a.Answer_mode,
                               a.Choice,
                               a.Ex_question,
                               a.Fill,
                               a.Iid,
                               a.Iorder,
                               a.Judge,
                               a.QnA
                           }).ToArray();
                //赋值外表信息
                question.chapter = exq.Chapter;
                question.content = exq.Econtent;
                question.course = exq.Course.Cname;
                question.creator = exq.Teacher.Tname;
                question.eid = exq.Eid;
                question.includeNum = exq.Enum;
                question.knowledge = exq.Knowledge;
                question.time = exq.Etime;
                question.TorE = exq.TorE;
                question.type = exq.Etype;
                //赋值内表信息
                question.inq = new UpdateInQuestion[inq.Count()];//需要根据inq行数新增question.inq数组元素
                for (int i = 0; i < inq.Count(); i++)
                {
                    question.inq[i] = new UpdateInQuestion();//需要根据循环次数来构造question.inq数组元素
                    question.inq[i].iid = inq[i].Iid;
                    question.inq[i].Iorder = inq[i].Iorder;
                    question.inq[i].answerMode = inq[i].Answer_mode;
                    question.inq[i].isNew = false;//标记为不是新增
                    question.inq[i].isDelete = false;//标记为不是删除了的
                    if (inq[i].Answer_mode == "选择题")
                    {
                        question.inq[i].answer = inq[i].Choice.Answer;
                        question.inq[i].Icontent = inq[i].Choice.Ccontent;
                        question.inq[i].isSingle = inq[i].Choice.Issingle;
                        question.inq[i].option1 = inq[i].Choice.Option1;
                        question.inq[i].option2 = inq[i].Choice.Option2;
                        question.inq[i].option3 = inq[i].Choice.Option3;
                        question.inq[i].option4 = inq[i].Choice.Option4;
                        question.inq[i].option5 = inq[i].Choice.Option5;
                        question.inq[i].option6 = inq[i].Choice.Option6;
                        question.inq[i].option7 = inq[i].Choice.Option7;
                        question.inq[i].option8 = inq[i].Choice.Option8;
                        question.inq[i].optionNum = inq[i].Choice.optionNum;
                    }
                    else if (inq[i].Answer_mode == "填空题")
                    {
                        question.inq[i].Icontent = inq[i].Fill.Fcontent;
                        question.inq[i].answer = inq[i].Fill.Answer;
                    }
                    else if (inq[i].Answer_mode == "判断题")
                    {
                        question.inq[i].answer = inq[i].Judge.Answer;
                        question.inq[i].Icontent = inq[i].Judge.Jcontent;
                    }
                    else if (inq[i].Answer_mode == "回答问题")
                    {
                        question.inq[i].answer = inq[i].QnA.Answer;
                        question.inq[i].Icontent = inq[i].QnA.Qcontent;
                    }
                }
                return question;
            }
            catch(Exception e)
            {
                return new UpdateQuestion();
            }
        }

        //增加试卷
        public bool AddPaper(NewPaper newPaper, PaperObject[] newPaperObject, PaperInfo[] newPaperInfo)
        {
            Paper paper = new Paper();//注意这些对象每用一次就要new一次
            Paper_object paperObject;
            Paper_info paperInfo;
            PEx_question pExq = new PEx_question();
            PIn_question pInq;
            PChoice pChoice;
            PJudge pJudge;
            PFill pFill;
            PQnA pQnA;
            Ex_question exq;
            In_question inq;
            Choice inqChoice;
            Judge inqJudge;
            Fill inqFill;
            QnA inqQnA;
            int currentEid = -1;
            try
            {
                //给试卷索引表的对象赋值
                paper.Course = db.Course.Where(a => a.Cname == newPaper.course).FirstOrDefault();
                paper.Create_time = newPaper.createTime;
                paper.Minute = newPaper.usedMinute;
                paper.Teacher = db.Teacher.Where(a => a.Tid == newPaper.creatorId).FirstOrDefault();
                paper.Title = newPaper.paperTitle;
                paper.TorE = newPaper.TorE;
                db.AddToPaper(paper);
                db.SaveChanges();
                //给每个试卷内容表的对象赋值
                if (newPaperObject != null)
                {
                    foreach (var temp in newPaperObject)
                    {
                        paperObject = new Paper_object();
                        pInq = new PIn_question();
                        exq = new Ex_question();
                        inq = new In_question();
                        exq = (from a in db.In_question
                               where a.Iid == temp.Iid
                               select a.Ex_question).FirstOrDefault();
                        inq = db.In_question.Where(a => a.Iid == temp.Iid).FirstOrDefault();
                        //从总试题索引表中复制一个副本到试卷名下的总试题索引表
                        if (currentEid != exq.Eid)
                        {
                            currentEid = exq.Eid;
                            pExq = new PEx_question();
                            pExq.Paper = paper;
                            pExq.Eid = exq.Eid;
                            pExq.Etime = exq.Etime;
                            pExq.Econtent = exq.Econtent;
                            pExq.Etype = exq.Etype;
                            pExq.Enum = exq.Enum;
                            pExq.Chapter = exq.Chapter;
                            pExq.Knowledge = exq.Knowledge;
                            pExq.TorE = exq.TorE;
                            db.AddToPEx_question(pExq);
                            db.SaveChanges();
                        }
                        //从小题索引表中复制一个副本到试卷名下的小题索引表
                        pInq.PEx_question = pExq;//如果pExq为空，会创建失败
                        pInq.Iid = inq.Iid;
                        pInq.Iorder = inq.Iorder;
                        pInq.Answer_mode = inq.Answer_mode;
                        db.AddToPIn_question(pInq);
                        db.SaveChanges();
                        //从各题型表中复制一个副本到试卷名下的各题型表
                        if (inq.Answer_mode == "选择题")
                        {
                            pChoice = new PChoice();
                            inqChoice = new Choice();
                            inqChoice = db.Choice.Where(a => a.Iid == inq.Iid).FirstOrDefault();
                            pChoice.PIn_question = pInq;
                            pChoice.Answer = inqChoice.Answer;
                            pChoice.Ccontent = inqChoice.Ccontent;
                            pChoice.Issingle = inqChoice.Issingle;
                            pChoice.Option1 = inqChoice.Option1;
                            pChoice.Option2 = inqChoice.Option2;
                            pChoice.Option3 = inqChoice.Option3;
                            pChoice.Option4 = inqChoice.Option4;
                            pChoice.Option5 = inqChoice.Option5;
                            pChoice.Option6 = inqChoice.Option6;
                            pChoice.Option7 = inqChoice.Option7;
                            pChoice.Option8 = inqChoice.Option8;
                            pChoice.optionNum = inqChoice.optionNum;
                            db.AddToPChoice(pChoice);
                            db.SaveChanges();
                        }
                        else if (inq.Answer_mode == "填空题")
                        {
                            pFill = new PFill();
                            inqFill = new Fill();
                            inqFill = db.Fill.Where(a => a.Iid == inq.Iid).FirstOrDefault();
                            pFill.PIn_question = pInq;
                            pFill.Fcontent = inqFill.Fcontent;
                            pFill.Answer = inqFill.Answer;
                            db.AddToPFill(pFill);
                            db.SaveChanges();
                        }
                        else if (inq.Answer_mode == "判断题")
                        {
                            pJudge = new PJudge();
                            inqJudge = new Judge();
                            inqJudge = db.Judge.Where(a => a.Iid == inq.Iid).FirstOrDefault();
                            pJudge.PIn_question = pInq;
                            pJudge.Jcontent = inqJudge.Jcontent;
                            pJudge.Answer = inqJudge.Answer;
                            db.AddToPJudge(pJudge);
                            db.SaveChanges();
                        }
                        else if (inq.Answer_mode == "回答问题")
                        {
                            pQnA = new PQnA();
                            inqQnA = new QnA();
                            inqQnA = db.QnA.Where(a => a.Iid == inq.Iid).FirstOrDefault();
                            pQnA.PIn_question = pInq;
                            pQnA.Answer = inqQnA.Answer;
                            pQnA.Qcontent = inqQnA.Qcontent;
                            db.AddToPQnA(pQnA);
                            db.SaveChanges();
                        }
                        //给试卷内容表的对象赋值
                        paperObject.Iid = pInq.Iid;//关联到副本的表
                        paperObject.Paper = paper;
                        paperObject.Point = temp.point;
                        paperObject.POorder = temp.POorder;
                        db.AddToPaper_object(paperObject);
                        db.SaveChanges();
                    }
                }
                //给每个试卷提示信息表的对象赋值
                if (newPaperInfo != null)
                {
                    foreach (var temp in newPaperInfo)
                    {
                        paperInfo = new Paper_info();
                        paperInfo.Before = temp.before;
                        paperInfo.Paper = paper;
                        paperInfo.PIorder = temp.PIorder;
                        paperInfo.PIcontent = temp.PIcontent;
                        db.AddToPaper_info(paperInfo);
                        db.SaveChanges();
                    }
                }
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }

        //删除试卷
        public bool DeletePaper(string pidString)
        {
            try
            {
                //处理接收到的pid字符串
                string[] pidArray1 = pidString.Split(' ');
                int[] pidArray2 = new int[pidArray1.Count()];
                for (int i = 0; i < pidArray1.Count(); i++)
                {
                    pidArray2[i] = Convert.ToInt32(pidArray1[i]);
                }
                foreach (int pid in pidArray2)
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        var paper = db.Paper.Where(a => a.Pid == pid).FirstOrDefault();
                        var paperObject = db.Paper_object.Where(a => a.Pid == pid).ToList();
                        var paperInfo = db.Paper_info.Where(a => a.Pid == pid).ToList();
                        var pExq = db.PEx_question.Where(a => a.Pid == pid).ToList();
                        var pInq = db.PIn_question.Where(a => a.Pid == pid).ToList();
                        var pChoice = db.PChoice.Where(a => a.Pid == pid).ToList();
                        var pFill = db.PFill.Where(a => a.Pid == pid).ToList();
                        var pJudge = db.PJudge.Where(a => a.Pid == pid).ToList();
                        var pQna = db.PQnA.Where(a => a.Pid == pid).ToList();
                        //删除与该试卷相关的用户答案（级联删除）
                        if (paper.TorE == "考试")
                        {
                            var answer = db.Exam_Answer.Where(a => a.Pid == paper.Pid);
                            foreach (var temp in answer)
                                db.DeleteObject(temp);
                        }
                        else if (paper.TorE == "练习")
                        {
                            var answer = db.Exercise_Answer.Where(a => a.Pid == paper.Pid);
                            foreach (var temp in answer)
                                db.DeleteObject(temp);
                        }
                        //删除提示信息
                        if (paperInfo.Count() > 0)
                        {
                            foreach (var temp in paperInfo)
                                db.DeleteObject(temp);
                        }
                        //删除试卷内容
                        if (paperObject.Count() > 0)
                        {
                            foreach (var temp in paperObject)
                                db.DeleteObject(temp);
                        }
                        //删除该试卷名下的题目副本
                        if (pChoice.Count() > 0)
                        {
                            foreach (var temp in pChoice)
                                db.DeleteObject(temp);
                        }
                        if (pFill.Count() > 0)
                        {
                            foreach (var temp in pFill)
                                db.DeleteObject(temp);
                        }
                        if (pJudge.Count() > 0)
                        {
                            foreach (var temp in pJudge)
                                db.DeleteObject(temp);
                        }
                        if (pQna.Count() > 0)
                        {
                            foreach (var temp in pQna)
                                db.DeleteObject(temp);
                        }
                        if (pInq.Count() > 0)
                        {
                            foreach (var temp in pInq)
                                db.DeleteObject(temp);
                        }
                        if (pExq.Count() > 0)
                        {
                            foreach (var temp in pExq)
                                db.DeleteObject(temp);
                        }
                        //删除试卷索引
                        db.DeleteObject(paper);
                        db.SaveChanges(false);
                        scope.Complete();
                        db.AcceptAllChanges();
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //获取准备修改试卷的信息
        public UpdatePaper GetUpdatePaper(int pid)
        {
            try
            {
                UpdatePaper paper = new UpdatePaper();
                var exp = (from a in db.Paper
                           where a.Pid == pid
                           select new
                           {
                               a.Course.Cname,
                               a.Create_time,
                               a.Minute,
                               a.Pid,
                               a.Teacher.Tid,
                               a.Title,
                               a.TorE
                           }).FirstOrDefault();
                var inpo = (from a in db.Paper_object
                            from b in db.PIn_question
                            where a.Pid == pid && b.Pid == pid && a.Iid == b.Iid
                            select new
                            {
                                //试卷内容表相关内容
                                a.Iid,
                                a.Point,
                                a.POorder,
                                //题目外表相关信息
                                b.PEx_question.Eid,
                                b.PEx_question.Econtent,
                                b.PEx_question.Etype,
                                //题目内表相关信息
                                b.Iorder,
                                b.Answer_mode,
                                b.PChoice,
                                b.PFill,
                                b.PJudge,
                                b.PQnA
                            }).ToArray();
                var inpi = (from a in db.Paper_info
                            where a.Pid == pid
                            select new
                            {
                                a.Before,
                                a.PIcontent,
                                a.PIorder
                            }).ToArray();
                //赋值外表（试卷索引表）信息
                paper.course = exp.Cname;
                paper.createTime = exp.Create_time;
                paper.creatorId = exp.Tid;
                paper.pid = pid;
                paper.TorE = exp.TorE;
                paper.usedMinute = exp.Minute;
                paper.paperTitle = exp.Title;
                //赋值内表（试卷内容表）信息
                paper.paperObject = new UpdatePaperObject[inpo.Count()];//需要根据inpo行数新增paper.paperObject数组元素
                for (int i = 0; i < inpo.Count(); i++)
                {
                    paper.paperObject[i] = new UpdatePaperObject();//需要根据循环次数来构造paper.paperObject数组元素
                    //试卷内容表相关内容
                    paper.paperObject[i].POorder = inpo[i].POorder;
                    paper.paperObject[i].Iid = inpo[i].Iid;
                    paper.paperObject[i].point = inpo[i].Point;
                    //题目外表相关信息
                    paper.paperObject[i].eid = inpo[i].Eid;
                    paper.paperObject[i].content = inpo[i].Econtent;
                    paper.paperObject[i].type = inpo[i].Etype;
                    //题目内表相关信息
                    paper.paperObject[i].Iorder = inpo[i].Iorder;
                    paper.paperObject[i].answerMode = inpo[i].Answer_mode;
                    if (inpo[i].Answer_mode == "选择题")
                    {
                        paper.paperObject[i].Icontent = inpo[i].PChoice.Ccontent;
                        paper.paperObject[i].answer = inpo[i].PChoice.Answer;
                        paper.paperObject[i].option1 = inpo[i].PChoice.Option1;
                        paper.paperObject[i].option2 = inpo[i].PChoice.Option2;
                        paper.paperObject[i].option3 = inpo[i].PChoice.Option3;
                        paper.paperObject[i].option4 = inpo[i].PChoice.Option4;
                        paper.paperObject[i].option5 = inpo[i].PChoice.Option5;
                        paper.paperObject[i].option6 = inpo[i].PChoice.Option6;
                        paper.paperObject[i].option7 = inpo[i].PChoice.Option7;
                        paper.paperObject[i].option8 = inpo[i].PChoice.Option8;
                        paper.paperObject[i].optionNum = inpo[i].PChoice.optionNum;
                        paper.paperObject[i].isSingle = inpo[i].PChoice.Issingle;
                    }
                    else if (inpo[i].Answer_mode == "填空题")
                    {
                        paper.paperObject[i].Icontent = inpo[i].PFill.Fcontent;
                        paper.paperObject[i].answer = inpo[i].PFill.Answer;
                    }
                    else if (inpo[i].Answer_mode == "判断题")
                    {
                        paper.paperObject[i].Icontent = inpo[i].PJudge.Jcontent;
                        paper.paperObject[i].answer = inpo[i].PJudge.Answer;
                    }
                    else if (inpo[i].Answer_mode == "回答问题")
                    {
                        paper.paperObject[i].Icontent = inpo[i].PQnA.Qcontent;
                        paper.paperObject[i].answer = inpo[i].PQnA.Answer;
                    }
                    paper.paperObject[i].isNew = false;//标记为不是新增
                    paper.paperObject[i].isDelete = false;//标记为不是删除了的
                }
                //赋值内表（试卷提示信息表）信息
                paper.paperInfo = new UpdatePaperInfo[inpi.Count()];//需要根据inpo行数新增paper.paperInfo数组元素
                for (int i = 0; i < inpi.Count(); i++)
                {
                    paper.paperInfo[i] = new UpdatePaperInfo();//需要根据循环次数来构造paper.paperInfo数组元素
                    paper.paperInfo[i].before = inpi[i].Before;
                    paper.paperInfo[i].PIcontent = inpi[i].PIcontent;
                    paper.paperInfo[i].PIorder = inpi[i].PIorder;
                    paper.paperInfo[i].isNew = false;//标记为不是新增
                    paper.paperInfo[i].isDelete = false;//标记为不是删除了的
                }
                return paper;
            }
            catch(Exception e)
            {
                //防止从地址栏输入不存在的id，都赋值为空
                return new UpdatePaper() { course="", createTime=new DateTime(), creatorId="", paperInfo=new UpdatePaperInfo[0], paperObject=new UpdatePaperObject[0], paperTitle="", pid=-1, TorE="", usedMinute=0 };
            }
        }

        //检查试卷是否有被学生做过
        public bool IsPaperUsed(int pid)
        {
            try
            {
                bool isUsed = false;
                int examCount = db.Exam_Answer.Where(a => a.Pid == pid).Count();
                int exerciseCount = db.Exercise_Answer.Where(a => a.Pid == pid).Count();
                if (examCount > 0 || exerciseCount > 0)
                {
                    isUsed = true;
                }
                return isUsed;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //修改试卷
        public bool UpdatePaper(UpdatePaperPost paper,UpdatePaperObjectPost[] paperObject,UpdatePaperInfo[] paperInfo)
        {
            try
            {
                //用于更新信息
                //Paper_object newPaperObject;
                //Paper_info newPaperInfo;
                int pOorder;
                int pIorder;
                //更新试卷索引表
                var exp = db.Paper.Where(a => a.Pid == paper.pid).FirstOrDefault();//这样得来的exq关联信息会空
                exp.Course = db.Course.Where(a => a.Cname == paper.course).FirstOrDefault();
                exp.Create_time = paper.createTime;
                exp.Minute = paper.usedMinute;
                exp.Teacher = db.Teacher.Where(a => a.Tid == paper.creatorId).FirstOrDefault();
                exp.Title = paper.paperTitle;
                exp.TorE = paper.TorE;
                db.SaveChanges();
                //更新试卷内容表
                if (paperObject != null)
                {
                    for (int i = 0; i < paperObject.Count(); i++)
                    {
                        pOorder = paperObject[i].POorder;
                        //newPaperObject = new Paper_object();
                        Paper_object newPaperObject = db.Paper_object.Where(a => a.Pid == paper.pid && a.POorder == pOorder).FirstOrDefault();
                        newPaperObject.Point = paperObject[i].point;
                        db.SaveChanges();
                    }
                }
                //更新试卷提示信息表
                if (paperInfo != null)
                {
                    for (int i = 0; i < paperInfo.Count(); i++)
                    {
                        pIorder = paperInfo[i].PIorder;
                        //newPaperInfo = new Paper_info();
                        Paper_info newPaperInfo = db.Paper_info.Where(a => a.Pid == paper.pid && a.PIorder == pIorder).FirstOrDefault();
                        newPaperInfo.PIcontent = paperInfo[i].PIcontent;
                        db.SaveChanges();
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        //下载word文档（TODO可以加个try catch）
        public FileStream DownloadPaper(int pid)
        {
            //用于下面一些方法的空值
            Object nothing = System.Reflection.Missing.Value;
            //记录创建的文档保存的文件夹、试卷的名称、文档的完整名称（完整路径）
            string downloadDir = AppDomain.CurrentDomain.BaseDirectory + "Content\\download\\";
            string paperTitle = db.Paper.Where(a => a.Pid == pid).FirstOrDefault().Title;
            object filename = downloadDir + paperTitle + ".doc";
            //获取整张试卷的信息
            UpdatePaper paper = GetUpdatePaper(pid);
            UpdatePaperObject[] paperObject = paper.paperObject;
            UpdatePaperInfo[] paperInfo = paper.paperInfo;
            //================================组建一个word文档=======================================
            //创建一个名为WordApp的组件对象 
            Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass();
            //创建一个名为WordDoc的文档对象 
            Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
            //添加试卷标题到文档
            Paragraph titleParagraph;
            titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
            titleParagraph.Range.Text = paperTitle;
            titleParagraph.Range.Font.Bold = 1;
            titleParagraph.Range.Font.Size = 20;
            titleParagraph.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            titleParagraph.Range.InsertParagraphAfter();
            titleParagraph.Range.Font.Bold = 0;
            titleParagraph.Range.Font.Size = 11;
            titleParagraph.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            //添加试卷正文内容到文档
            int preid = -1;//记录前一大题id，初始-1代表无
            int exid;//记录当前大题id
            int i = 0;//记录当前paperInfo的数组id
            int j;//记录当前paperObject的数组id
            int order = 1;//记录试卷题序
            //循环输出每一小题
            if (paperObject != null)
            {
                for (j = 0; j < paperObject.Count(); j++)
                {
                    exid = paperObject[j].eid;
                    //判断是否要在小题前插入信息
                    if (paperInfo != null)
                    {
                        while (i < paperInfo.Count() && paperInfo[i].before == paperObject[j].POorder.ToString())
                        {
                            //插入paperInfo[i].PIcontent到文档
                            titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                            titleParagraph.Range.InsertParagraphBefore();
                            titleParagraph.Range.Text = paperInfo[i].PIcontent;
                            titleParagraph.Range.InsertParagraphAfter();

                            i++;
                        }
                    }
                    //判断是否要输出大题题干
                    if (preid != exid)
                    {
                        preid = exid;
                        //插入paperObject[j].content大题题干到文档
                        titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                        titleParagraph.Range.Text = paperObject[j].content;
                        titleParagraph.Range.InsertParagraphAfter();
                    }
                    //判断题目类型再输出小题
                    if (paperObject[j].answerMode == "选择题")
                    {
                        //插入paperObject[j].Icontent小题题干到文档
                        if (paperObject[j].Icontent != null)
                        {
                            titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                            titleParagraph.Range.Text = (order++) + ". " + paperObject[j].Icontent;
                            titleParagraph.Range.InsertParagraphAfter();
                            if (paperObject[j].option1 != null)
                            {
                                titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                                titleParagraph.Range.Text = "A." + paperObject[j].option1;
                                titleParagraph.Range.InsertParagraphAfter();
                            }
                            if (paperObject[j].option2 != null)
                            {
                                titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                                titleParagraph.Range.Text = "B." + paperObject[j].option2;
                                titleParagraph.Range.InsertParagraphAfter();
                            }
                            if (paperObject[j].option3 != null)
                            {
                                titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                                titleParagraph.Range.Text = "C." + paperObject[j].option3;
                                titleParagraph.Range.InsertParagraphAfter();
                            }
                            if (paperObject[j].option4 != null)
                            {
                                titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                                titleParagraph.Range.Text = "D." + paperObject[j].option4;
                                titleParagraph.Range.InsertParagraphAfter();
                            }
                            if (paperObject[j].option5 != null)
                            {
                                titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                                titleParagraph.Range.Text = "E." + paperObject[j].option5;
                                titleParagraph.Range.InsertParagraphAfter();
                            }
                            if (paperObject[j].option6 != null)
                            {
                                titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                                titleParagraph.Range.Text = "F." + paperObject[j].option6;
                                titleParagraph.Range.InsertParagraphAfter();
                            }
                            if (paperObject[j].option7 != null)
                            {
                                titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                                titleParagraph.Range.Text = "G." + paperObject[j].option7;
                                titleParagraph.Range.InsertParagraphAfter();
                            }
                            if (paperObject[j].option8 != null)
                            {
                                titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                                titleParagraph.Range.Text = "H." + paperObject[j].option8;
                                titleParagraph.Range.InsertParagraphAfter();
                            }
                        }
                        else
                        {
                            string select = "";
                            if (paperObject[j].option1 != null)
                            {
                                select += (order++) + ". A." + paperObject[j].option1 + " ";
                            }
                            if (paperObject[j].option2 != null)
                            {
                                select += "B." + paperObject[j].option2 + " ";
                            }
                            if (paperObject[j].option3 != null)
                            {
                                select += "C." + paperObject[j].option3 + " ";
                            }
                            if (paperObject[j].option4 != null)
                            {
                                select += "D." + paperObject[j].option4 + " ";
                            }
                            if (paperObject[j].option5 != null)
                            {
                                select += "E." + paperObject[j].option5 + " ";
                            }
                            if (paperObject[j].option6 != null)
                            {
                                select += "F." + paperObject[j].option6 + " ";
                            }
                            if (paperObject[j].option7 != null)
                            {
                                select += "G." + paperObject[j].option7 + " ";
                            }
                            if (paperObject[j].option8 != null)
                            {
                                select += "H." + paperObject[j].option8 + " ";
                            }
                            titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                            titleParagraph.Range.Text = select;
                            titleParagraph.Range.InsertParagraphAfter();
                        }
                    }
                    else if (paperObject[j].answerMode == "填空题")
                    {
                        //插入paperObject[j].Icontent小题题干到文档
                        if (paperObject[j].Icontent != null)
                        {
                            titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                            titleParagraph.Range.Text = (order++) + ". " + paperObject[j].Icontent;
                            titleParagraph.Range.InsertParagraphAfter();
                        }
                    }
                    else if (paperObject[j].answerMode == "判断题")
                    {
                        //插入paperObject[j].Icontent小题题干到文档
                        if (paperObject[j].Icontent != null)
                        {
                            titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                            titleParagraph.Range.Text = (order++) + ". " + paperObject[j].Icontent;
                            titleParagraph.Range.InsertParagraphAfter();
                        }
                    }
                    else if (paperObject[j].answerMode == "回答问题")
                    {
                        //插入paperObject[j].Icontent小题题干到文档
                        if (paperObject[j].Icontent != null)
                        {
                            titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                            titleParagraph.Range.Text = (order++) + ". " + paperObject[j].Icontent;
                            titleParagraph.Range.InsertParagraphAfter();
                        }
                    }
                }
            }
            titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
            titleParagraph.Range.InsertParagraphAfter();
            //输出所有末尾的信息
            if (paperInfo != null)
            {
                for (; i < paperInfo.Count(); i++)
                {
                    if (paperInfo[i].before.Equals("-1"))
                    {
                        //插入paperInfo[i].PIcontent到文档
                        titleParagraph = wordDoc.Content.Paragraphs.Add(ref nothing);
                        titleParagraph.Range.Text = paperInfo[i].PIcontent;
                        titleParagraph.Range.InsertParagraphAfter();
                    }
                }
            }


            //将WordDoc文档对象的内容保存为DOC文档 
            wordDoc.SaveAs(ref filename, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
            //关闭WordDoc文档对象 
            wordDoc.Close(ref nothing, ref nothing, ref nothing);
            //关闭WordApp组件对象 
            wordApp.Quit(ref nothing, ref nothing, ref nothing);

            FileStream fileStream = new FileStream(filename.ToString(), FileMode.Open);
            return fileStream;
        }

        //获取准备要做的试卷信息
        public PaperUsed GetPaperUsed(int pid)
        {
            try
            {
                PaperUsed paper = new PaperUsed();
                var exp = (from a in db.Paper
                           where a.Pid == pid
                           select new
                           {
                               a.Course.Cname,
                               a.Create_time,
                               a.Minute,
                               a.Pid,
                               a.Teacher.Tid,
                               a.Title,
                               a.TorE
                           }).FirstOrDefault();
                var inpo = (from a in db.Paper_object
                            from b in db.PIn_question
                            where a.Pid == pid && b.Pid == pid && a.Iid==b.Iid
                            select new
                            {
                                //试卷内容表相关内容
                                a.Iid,
                                a.Point,
                                a.POorder,
                                //题目外表相关信息
                                b.PEx_question.Eid,
                                b.PEx_question.Econtent,
                                b.PEx_question.Etype,
                                //题目内表相关信息
                                b.Iorder,
                                b.Answer_mode,
                                b.PChoice,
                                b.PFill,
                                b.PJudge,
                                b.PQnA
                            }).ToArray();
                var inpi = (from a in db.Paper_info
                            where a.Pid == pid
                            select new
                            {
                                a.Before,
                                a.PIcontent,
                                a.PIorder
                            }).ToArray();
                //赋值外表（试卷索引表）信息
                paper.course = exp.Cname;       
                paper.pid = pid;
                paper.TorE = exp.TorE;
                paper.usedMinute = exp.Minute;
                paper.paperTitle = exp.Title;
                //赋值内表（试卷内容表）信息
                paper.paperObjectUsed = new PaperObjectUsed[inpo.Count()];//需要根据inpo行数新增paper.paperObject数组元素
                for (int i = 0; i < inpo.Count(); i++)
                {
                    paper.paperObjectUsed[i] = new PaperObjectUsed();//需要根据循环次数来构造paper.paperObject数组元素
                    //试卷内容表相关内容
                    paper.paperObjectUsed[i].POorder = inpo[i].POorder;
                    paper.paperObjectUsed[i].Iid = inpo[i].Iid;
                    paper.paperObjectUsed[i].point = inpo[i].Point;
                    //题目外表相关信息
                    paper.paperObjectUsed[i].eid = inpo[i].Eid;
                    paper.paperObjectUsed[i].content = inpo[i].Econtent;
                    paper.paperObjectUsed[i].type = inpo[i].Etype;
                    //题目内表相关信息
                    paper.paperObjectUsed[i].Iorder = inpo[i].Iorder;
                    paper.paperObjectUsed[i].answerMode = inpo[i].Answer_mode;
                    if (inpo[i].Answer_mode == "选择题")
                    {
                        paper.paperObjectUsed[i].Icontent = inpo[i].PChoice.Ccontent;
                        paper.paperObjectUsed[i].option1 = inpo[i].PChoice.Option1;
                        paper.paperObjectUsed[i].option2 = inpo[i].PChoice.Option2;
                        paper.paperObjectUsed[i].option3 = inpo[i].PChoice.Option3;
                        paper.paperObjectUsed[i].option4 = inpo[i].PChoice.Option4;
                        paper.paperObjectUsed[i].option5 = inpo[i].PChoice.Option5;
                        paper.paperObjectUsed[i].option6 = inpo[i].PChoice.Option6;
                        paper.paperObjectUsed[i].option7 = inpo[i].PChoice.Option7;
                        paper.paperObjectUsed[i].option8 = inpo[i].PChoice.Option8;
                        paper.paperObjectUsed[i].optionNum = inpo[i].PChoice.optionNum;
                        paper.paperObjectUsed[i].isSingle = inpo[i].PChoice.Issingle;
                    }
                    else if (inpo[i].Answer_mode == "填空题")
                    {
                        paper.paperObjectUsed[i].Icontent = inpo[i].PFill.Fcontent;
                    }
                    else if (inpo[i].Answer_mode == "判断题")
                    {
                        paper.paperObjectUsed[i].Icontent = inpo[i].PJudge.Jcontent;
                    }
                    else if (inpo[i].Answer_mode == "回答问题")
                    {
                        paper.paperObjectUsed[i].Icontent = inpo[i].PQnA.Qcontent;
                    }
                }
                //赋值内表（试卷提示信息表）信息
                paper.paperInfoUsed = new PaperInfoUsed[inpi.Count()];//需要根据inpo行数新增paper.paperInfo数组元素
                for (int i = 0; i < inpi.Count(); i++)
                {
                    paper.paperInfoUsed[i] = new PaperInfoUsed();//需要根据循环次数来构造paper.paperInfo数组元素
                    paper.paperInfoUsed[i].before = inpi[i].Before;
                    paper.paperInfoUsed[i].PIcontent = inpi[i].PIcontent;
                    paper.paperInfoUsed[i].PIorder = inpi[i].PIorder;
                }
                return paper;
            }
            catch (Exception e)
            {
                //防止从地址栏输入不存在的id，都赋值为空
                return new PaperUsed() { beginTime=new DateTime(), course="", paperInfoUsed=new PaperInfoUsed[0], paperObjectUsed=new PaperObjectUsed[0], paperTitle="", pid=-1, TorE="", usedMinute=0};
            }
        }

        //提交试卷的答案
        public bool PostPaperAnswer(ExPaperAnswer exPaperAnswer)
        {
            try 
            {
                Exam_Answer exam;
                Exercise_Answer exercise;
                int pid;
                int POorder;
                string sid;
                pid = exPaperAnswer.pid;
                sid = exPaperAnswer.userId;
                if (exPaperAnswer.paperAnswer != null)
                {
                    for (int i = 0; i < exPaperAnswer.paperAnswer.Count(); i++)
                    {
                        POorder = exPaperAnswer.paperAnswer[i].POorder;
                        if (exPaperAnswer.TorE == "考试")
                        {
                            exam = new Exam_Answer();
                            exam.Answer = exPaperAnswer.paperAnswer[i].answer;
                            exam.Begintime = exPaperAnswer.beginTime;
                            exam.Paper_object = db.Paper_object.Where(a => a.Pid == pid && a.POorder == POorder).FirstOrDefault();
                            exam.Student = db.Student.Where(a => a.Sid == sid).FirstOrDefault();
                            db.AddToExam_Answer(exam);
                            db.SaveChanges();
                        }
                        else
                        {
                            exercise = new Exercise_Answer();
                            exercise.Answer = exPaperAnswer.paperAnswer[i].answer;
                            exercise.Begintime = exPaperAnswer.beginTime;
                            exercise.Paper_object = db.Paper_object.Where(a => a.Pid == pid && a.POorder == POorder).FirstOrDefault();
                            exercise.Student = db.Student.Where(a => a.Sid == sid).FirstOrDefault();
                            db.AddToExercise_Answer(exercise);
                            db.SaveChanges();
                        }
                    }
                    return true;
                }
                else
                    return false;
            }
            catch(Exception e)
            {
                return false;
            }
        }

        //返回刚提交的练习或考试试卷的所填答案和参考答案
        public AnswerCompare CompareAnswer(AnswerQuery answerQuery, string TorE)
        {
            try
            {
                AnswerCompare exerciseCompare = new AnswerCompare();
                int poorder;
                double? attainScore = 0.0;//记录获得分数
                double? totalPoint = 0.0;//记录试卷的满分
                var inpo = (from a in db.Paper_object
                            from b in db.PIn_question
                            where a.Pid == answerQuery.pid && b.Pid==answerQuery.pid && a.Iid==b.Iid
                            select new
                            {
                                //试卷内容表相关内容
                                a.Iid,
                                a.Point,
                                a.POorder,
                                //题目外表相关信息
                                b.PEx_question.Eid,
                                b.PEx_question.Econtent,
                                b.PEx_question.Etype,
                                //题目内表相关信息
                                b.Iorder,
                                b.Answer_mode,
                                b.PChoice,
                                b.PFill,
                                b.PJudge,
                                b.PQnA
                            }).ToArray();
                var inpi = (from a in db.Paper_info
                            where a.Pid == answerQuery.pid
                            select new
                            {
                                a.Before,
                                a.PIcontent,
                                a.PIorder
                            }).ToArray();
                //用于对比练习答案
                var exera = (from a in db.Exercise_Answer
                             where a.Pid == answerQuery.pid && a.Sid == answerQuery.userId && a.Begintime == answerQuery.beginTime
                             select new
                             {
                                 a.POorder,
                                 a.Answer
                             });
                //用于对比考试答案
                var exama = (from a in db.Exam_Answer
                             where a.Pid == answerQuery.pid && a.Sid == answerQuery.userId && a.Begintime == answerQuery.beginTime
                             select new
                             {
                                 a.POorder,
                                 a.Answer,
                                 a.Score
                             });
                //赋值外表（试卷索引表）信息
                exerciseCompare.userId = answerQuery.userId;
                exerciseCompare.beginTime = answerQuery.beginTime;
                exerciseCompare.pid = answerQuery.pid;
                exerciseCompare.paperTitle = db.Paper.Where(a => a.Pid == answerQuery.pid).FirstOrDefault().Title;
                //赋值内表（试卷内容表）信息
                exerciseCompare.epaperObject = new AnswerPaperObject[inpo.Count()];//需要根据inpo行数新增paper.paperObject数组元素
                for (int i = 0; i < inpo.Count(); i++)
                {
                    totalPoint += inpo[i].Point;//统计试卷满分（TODO可以改进在试卷录入时就统计好的）
                    exerciseCompare.epaperObject[i] = new AnswerPaperObject();//需要根据循环次数来构造paper.paperObject数组元素
                    //试卷内容表相关内容
                    exerciseCompare.epaperObject[i].POorder = inpo[i].POorder;
                    exerciseCompare.epaperObject[i].Iid = inpo[i].Iid;
                    exerciseCompare.epaperObject[i].point = inpo[i].Point;
                    //题目外表相关信息
                    exerciseCompare.epaperObject[i].eid = inpo[i].Eid;
                    exerciseCompare.epaperObject[i].content = inpo[i].Econtent;
                    exerciseCompare.epaperObject[i].type = inpo[i].Etype;
                    //题目内表相关信息
                    exerciseCompare.epaperObject[i].Iorder = inpo[i].Iorder;
                    exerciseCompare.epaperObject[i].answerMode = inpo[i].Answer_mode;
                    //获取该小题所填答案（从练习答案表或考试答案表获取，根据TorE的值选择）
                    poorder = inpo[i].POorder;
                    if (TorE == "练习")
                    {
                        exerciseCompare.epaperObject[i].userAnswer = exera.Where(a => a.POorder == poorder).FirstOrDefault().Answer;
                    }
                    else
                    {
                        exerciseCompare.epaperObject[i].userAnswer = exama.Where(a => a.POorder == poorder).FirstOrDefault().Answer;
                        exerciseCompare.epaperObject[i].score = exama.Where(a => a.POorder == poorder).FirstOrDefault().Score;
                    }
                    if (inpo[i].Answer_mode == "选择题")
                    {
                        exerciseCompare.epaperObject[i].Icontent = inpo[i].PChoice.Ccontent;
                        exerciseCompare.epaperObject[i].answer = inpo[i].PChoice.Answer;
                        exerciseCompare.epaperObject[i].option1 = inpo[i].PChoice.Option1;
                        exerciseCompare.epaperObject[i].option2 = inpo[i].PChoice.Option2;
                        exerciseCompare.epaperObject[i].option3 = inpo[i].PChoice.Option3;
                        exerciseCompare.epaperObject[i].option4 = inpo[i].PChoice.Option4;
                        exerciseCompare.epaperObject[i].option5 = inpo[i].PChoice.Option5;
                        exerciseCompare.epaperObject[i].option6 = inpo[i].PChoice.Option6;
                        exerciseCompare.epaperObject[i].option7 = inpo[i].PChoice.Option7;
                        exerciseCompare.epaperObject[i].option8 = inpo[i].PChoice.Option8;
                        exerciseCompare.epaperObject[i].optionNum = inpo[i].PChoice.optionNum;
                        exerciseCompare.epaperObject[i].isSingle = inpo[i].PChoice.Issingle;
                        //判定该小题是否答对
                        if (exerciseCompare.epaperObject[i].userAnswer != null)
                        {
                            if (exerciseCompare.epaperObject[i].userAnswer.Equals(exerciseCompare.epaperObject[i].answer))
                            {
                                exerciseCompare.epaperObject[i].isRight = "right";
                                attainScore += exerciseCompare.epaperObject[i].point;//答对就统计得分
                            }
                            else
                            {
                                exerciseCompare.epaperObject[i].isRight = "wrong";
                            }
                        }
                        else
                        {
                            exerciseCompare.epaperObject[i].isRight = "wrong";
                        }
                    }
                    else if (inpo[i].Answer_mode == "填空题")
                    {
                        exerciseCompare.epaperObject[i].Icontent = inpo[i].PFill.Fcontent;
                        exerciseCompare.epaperObject[i].answer = inpo[i].PFill.Answer;
                        //判定该小题是否答对
                        if (exerciseCompare.epaperObject[i].userAnswer != null)
                        {
                            if (exerciseCompare.epaperObject[i].userAnswer.Equals(exerciseCompare.epaperObject[i].answer))
                            {
                                exerciseCompare.epaperObject[i].isRight = "right";
                                attainScore += exerciseCompare.epaperObject[i].point;//答对就统计得分
                            }
                            else
                            {
                                exerciseCompare.epaperObject[i].isRight = "uncertain";
                            }
                        }
                        else
                        {
                            exerciseCompare.epaperObject[i].isRight = "wrong";
                        }
                    }
                    else if (inpo[i].Answer_mode == "判断题")
                    {
                        exerciseCompare.epaperObject[i].Icontent = inpo[i].PJudge.Jcontent;
                        exerciseCompare.epaperObject[i].answer = inpo[i].PJudge.Answer;
                        //判定该小题是否答对
                        if (exerciseCompare.epaperObject[i].userAnswer != null)
                        {
                            if (exerciseCompare.epaperObject[i].userAnswer.Equals(exerciseCompare.epaperObject[i].answer))
                            {
                                exerciseCompare.epaperObject[i].isRight = "right";
                                attainScore += exerciseCompare.epaperObject[i].point;//答对就统计得分
                            }
                            else
                            {
                                exerciseCompare.epaperObject[i].isRight = "wrong";
                            }
                        }
                        else
                        {
                            exerciseCompare.epaperObject[i].isRight = "wrong";
                        }
                    }
                    else if (inpo[i].Answer_mode == "回答问题")
                    {
                        exerciseCompare.epaperObject[i].Icontent = inpo[i].PQnA.Qcontent;
                        exerciseCompare.epaperObject[i].answer = inpo[i].PQnA.Answer;
                        //判定该小题是否答对
                        if (exerciseCompare.epaperObject[i].userAnswer != null)
                        {
                            if (exerciseCompare.epaperObject[i].userAnswer.Equals(exerciseCompare.epaperObject[i].answer))
                            {
                                exerciseCompare.epaperObject[i].isRight = "right";
                                attainScore += exerciseCompare.epaperObject[i].point;//答对就统计得分
                            }
                            else
                            {
                                exerciseCompare.epaperObject[i].isRight = "uncertain";
                            }
                        }
                        else
                        {
                            exerciseCompare.epaperObject[i].isRight = "wrong";
                        }
                    }
                }
                //赋值内表（试卷提示信息表）信息
                exerciseCompare.epaperInfo = new PaperInfoUsed[inpi.Count()];//需要根据inpo行数新增paper.paperInfo数组元素
                for (int i = 0; i < inpi.Count(); i++)
                {
                    exerciseCompare.epaperInfo[i] = new PaperInfoUsed();//需要根据循环次数来构造paper.paperInfo数组元素
                    exerciseCompare.epaperInfo[i].before = inpi[i].Before;
                    exerciseCompare.epaperInfo[i].PIcontent = inpi[i].PIcontent;
                    exerciseCompare.epaperInfo[i].PIorder = inpi[i].PIorder;
                }
                //赋值分数
                exerciseCompare.objectQuestionScore = attainScore;
                exerciseCompare.paperTotalPoint = totalPoint;

                return exerciseCompare;
            }
            catch(Exception e)
            {
                //防止从地址栏输入不存在的id，都赋值为空
                return new AnswerCompare() { beginTime=new DateTime(), epaperInfo=new PaperInfoUsed[0], epaperObject=new AnswerPaperObject[0], objectQuestionScore=0, paperTitle="", paperTotalPoint=0, pid=-1, TorE="", userId=""};
            }
        }

        //根据试卷ID返回做过该试卷的用户信息
        public List<ExamUserInfo> GetExamUser(ExamUserInfo info)
        {
            try
            {
                double? paperScore = 0;
                int id = int.Parse(info.pid);
                var list = (from a in db.Exam_Answer
                            where a.Pid == id
                            select new ExamUserInfo
                            {
                                beginTime = a.Begintime,
                                pid = info.pid,
                                userId = a.Sid,
                                sName = a.Student.Sname,
                                isCorrected = "未批改",
                                score = a.Score
                            }).ToList<ExamUserInfo>().Distinct(new ExamUserInfoComparer());
                foreach (var temp in list)
                {
                    if (temp.score != null)
                    {
                        paperScore = 0;
                        var rows = db.Exam_Answer.Where(a => a.Pid == id && a.Sid == temp.userId && a.Begintime == temp.beginTime);
                        foreach (var row in rows)
                        {
                            paperScore += row.Score;
                        }
                        temp.isCorrected = "已批改(" + paperScore.ToString() + "分)";
                    }
                }
                if (info.isCorrected != null)
                {
                    list = list.Where(a => a.isCorrected.Contains(info.isCorrected));
                }
                if (info.userId != null)
                {
                    list = list.Where(a => a.userId.Contains(info.userId));
                }
                if (info.sName != null)
                {
                    list = list.Where(a => a.sName.Contains(info.sName));
                }
                return list.ToList();
                /*
                var list = (from a in db.Exam_Answer
                            where a.Pid == info.pid
                            select new ExamUserInfo
                            {
                                beginTime = a.Begintime,
                                pid = a.Pid,
                                userId = a.Sid,
                                sName = a.Student.Sname,
                                isCorrected = "未批改",
                                score = a.Score
                            }).Distinct();
                foreach (var temp in list)
                {
                    if (temp.score != null)
                    {
                        temp.isCorrected = "已批改";
                    }
                }
                if (info.isCorrected != null)
                {
                    list = list.Where(a => a.isCorrected == info.isCorrected);
                }
                if (info.userId != null)
                {
                    list = list.Where(a => a.userId.Contains(info.userId));
                }
                if (info.sName != null)
                {
                    list = list.Where(a => a.sName.Contains(info.sName));
                }
                return list.ToList();
              * */
            }
            catch(Exception e)
            {
                return new List<ExamUserInfo>();
            }
        }

        //录入考试试卷的分数
        public bool FillScore(ExPaperAnswer answer)
        {
            try
            {
                int order;
                for (int i = 0; i < answer.paperAnswer.Count(); i++)
                {
                    order = answer.paperAnswer[i].POorder;
                    var exama = db.Exam_Answer.Where(a => a.Pid == answer.pid && a.Sid == answer.userId && a.Begintime == answer.beginTime && a.POorder == order).FirstOrDefault();
                    exama.Score = answer.paperAnswer[i].score;
                    db.SaveChanges();
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
